///
//  LoadBase v 1.0
//  Russell Lowke, March 1st 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//

function LoadBase(url)                      // (string) url of file being loaded
{
    this.id;                                // (uint) each loader is given a unique numeric id
    this.assetLoader;                       // (AssetLoader) refrenece to asset loader
    this.url;                               // (string) url of file being loaded
    
    this.initialize = function(url)         // (string) url of file being loaded
    {
        this.assetLoader = AssetLoader.instance;
        this.url = url;
        this.id = LoadBase.idCount++;
        
        LogAs.debug("File requested (" + this.id + "): " + this.url, AssetLoader.DEBUG_FILE_REQUESTED);
    }
    
    //
    // close the loader before it gets a chance to finish loading
    this.close = function() 
    {
        LogAs.info("AssetLoader closing loading file (" + this.id + "):" + this.url, AssetLoader.INFO_FILE_CLOSED);
            
        // Note: removeAssetLoader() will call dispose() on this instance
        this.assetLoader.removeAssetLoader(this);
    }
    
    ///
    // all failed loads are logged as warnings
    this.loadFailed = function(errStr,              // (string) Error string describing warning
                               errID)               // (uint) ID of warning
    {
        // give warning that load failed
        LogAs.warning(errStr, errID);
        
        // dispatch asset as null
        var evt = $.Event(LoadBase.ASSET_LOADED_EVENT);
        evt.asset = null;                       // asset assigned as null indicating no asset
        $(this).trigger(evt);
            
        // remove loader
        this.assetLoader.removeAssetLoader(this);
    }
    
    this.dispose = function(retry)              // (boolean) true if attempting a retry
    {
        // dispose of this loader, called by removeAssetLoader() in AssetLoader
        this.cleanup();
            
        if (! retry) {
            var evt = $.Event(LoadBase.LOADER_DISPOSED_EVENT);
            $(this).trigger(evt);
        }
    }

    // startLoad must be overwritten by extending Loader object
    this.startLoad = function() 
    {
        throw new Error("startLoad() must be overridden");
    }
    
    // fileLoaded must be overwritten by extending Loader object
    this.fileLoaded = function() 
    {
        throw new Error("fileLoaded() must be overridden");
    }
        
    // cleanup must be overwritten by extending Loader object
    this.cleanup = function() 
    {
        throw new Error("cleanup() must be overridden");
    }
};

LoadBase.idCount = 0;           // (uint) used to create unique ids for AssetLoaders

// events dispatched
LoadBase.ASSET_LOADED_EVENT     = "asset_loaded_event";         // (string) event string indicating asset has loaded
LoadBase.LOADER_DISPOSED_EVENT  = "loader_disposed_event";      // (string) event string indicating loader was disposed
